﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;
namespace Sale_Operation
{
    public class BuyBillOp:Component
    {
        public override bool Save(object obj)
        {
            throw new NotImplementedException();
        }
        public bool Save(object obj, DataTable dtDetail)
        {
            bool blRes = false;
            BuyBill SM = obj as BuyBill;
            int ioldId = SM.BBill_Id;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                if (SM.BBill_Id == 0)
                {
                    sbSql.Append("INSERT INTO BuyBill (BBill_Code,BBill_Supp_Id,BBill_Date,BBill_Money,BBill_Count,BBill_SUser_Id,BBill_Input,BBilll_PayType,BBill_AList_Id,BBill_State,BBill_Memo,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append("BBill_Code='" + ValueHandler.GetStringValue(SM.BBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Supp_Id='" + ValueHandler.GetStringValue(SM.BBill_Supp_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Date='" + ValueHandler.GetStringValue(SM.BBill_Date, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Money='" + ValueHandler.GetStringValue(SM.BBill_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Count='" + ValueHandler.GetStringValue(SM.BBill_Count, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_SUser_Id='" + ValueHandler.GetStringValue(SM.BBill_SUser_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Input='" + ValueHandler.GetStringValue(SM.BBill_Input, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBilll_PayType='" + ValueHandler.GetStringValue(SM.BBilll_PayType, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_AList_Id='" + ValueHandler.GetStringValue(SM.BBill_AList_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_State='" + ValueHandler.GetStringValue(SM.BBill_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Memo='" + ValueHandler.GetStringValue(SM.BBill_Memo, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("State=1,");
                    sbSql.Append("CreateTime=getdate(),");
                    sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SM.BBill_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity ")); 
                }
                else
                {
                    sbSql.Append("UPDATE BuyBill SET ");
                    sbSql.Append("BBill_Code='" + ValueHandler.GetStringValue(SM.BBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Supp_Id='" + ValueHandler.GetStringValue(SM.BBill_Supp_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Date='" + ValueHandler.GetStringValue(SM.BBill_Date, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Money='" + ValueHandler.GetStringValue(SM.BBill_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Count='" + ValueHandler.GetStringValue(SM.BBill_Count, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_SUser_Id='" + ValueHandler.GetStringValue(SM.BBill_SUser_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBilll_PayType='" + ValueHandler.GetStringValue(SM.BBilll_PayType, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_AList_Id='" + ValueHandler.GetStringValue(SM.BBill_AList_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_State='" + ValueHandler.GetStringValue(SM.BBill_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Memo='" + ValueHandler.GetStringValue(SM.BBill_Memo, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE BBill_Id=" + SM.BBill_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString()); 
                }
                //新增明细信息
                sbSql = new StringBuilder();
                sbSql.Append(" DELETE FROM BuyBillDetail WHERE BBDeta_SBill_Id="+SM.BBill_Id);
                SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());                 
                for (int i = 0; i < dtDetail.Rows.Count; i++)
                {
                    if (dtDetail.Rows[i].RowState == DataRowState.Deleted)
                        continue;
                    sbSql = new StringBuilder();
                    sbSql.Append(" INSERT INTO BuyBillDetail(BBDeta_SBill_Id,BBDeta_PInfo_Id,BBDeta_Count,BBDeta_CostPrice,BBDeta_Price,BBDeta_Money,BBDeta_Memo,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append(" BBDeta_SBill_Id=" +SM.BBill_Id + ",");
                    sbSql.Append(" BBDeta_PInfo_Id=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["BBDeta_PInfo_Id"]) + ",");
                    sbSql.Append(" BBDeta_Count=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["BBDeta_Count"]) + ",");
                    sbSql.Append(" BBDeta_CostPrice=" + ValueHandler.GetDecNumberValue(dtDetail.Rows[i]["BBDeta_CostPrice"]).ToString() + ",");
                    sbSql.Append(" BBDeta_Price=" + ValueHandler.GetDecNumberValue(dtDetail.Rows[i]["BBDeta_Price"]).ToString() + ",");
                    sbSql.Append(" BBDeta_Money=" + ValueHandler.GetDecNumberValue(dtDetail.Rows[i]["BBDeta_Money"]).ToString() + ",");
                    sbSql.Append(" BBDeta_Memo='" + ValueHandler.GetStringValue(dtDetail.Rows[i]["BBDeta_Memo"], ValueHandler.CharHandlerType.IsRepChar).ToString() + "',");
                    sbSql.Append(" State=1,");
                    sbSql.Append(" CreateTime=getdate(),");
                    sbSql.Append(" CreateMan=" + SM.CreateMan);
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());  
                }
                db.Transaction.Commit();
                blRes = true;
            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增/修改进货单信息出错！[" + ex.ToString() + "]", EMenuList.进货单管理);
            }
            db.Close();
            if (ioldId == 0)
            {
                LinkService.InsertLog(ESysLogType.Operation, "新增进货单信息[" + ValueHandler.GetStringValue(SM.BBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.进货单管理);  
            }
            else
            {
                LinkService.InsertLog(ESysLogType.Operation, "修改进货单信息[" + ValueHandler.GetStringValue(SM.BBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.进货单管理); 
            }
            db.Close();
            return blRes;
        }
        public bool Update(object obj)
        {
            bool blRes = false;
            BuyBill SM = obj as BuyBill;
            int ioldId = SM.BBill_Id;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
          
                    sbSql.Append("UPDATE BuyBill SET ");
                    sbSql.Append("BBill_Code='" + ValueHandler.GetStringValue(SM.BBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Supp_Id='" + ValueHandler.GetStringValue(SM.BBill_Supp_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Date='" + ValueHandler.GetStringValue(SM.BBill_Date, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Money='" + ValueHandler.GetStringValue(SM.BBill_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Count='" + ValueHandler.GetStringValue(SM.BBill_Count, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_SUser_Id='" + ValueHandler.GetStringValue(SM.BBill_SUser_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBilll_PayType='" + ValueHandler.GetStringValue(SM.BBilll_PayType, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_AList_Id='" + ValueHandler.GetStringValue(SM.BBill_AList_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_State='" + ValueHandler.GetStringValue(SM.BBill_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BBill_Memo='" + ValueHandler.GetStringValue(SM.BBill_Memo, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE BBill_Id=" + SM.BBill_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString()); 
                }
            catch (Exception ex)
            { 
                LinkService.InsertLog(ESysLogType.Error, "新增/修改进货单信息出错！[" + ex.ToString() + "]", EMenuList.进货单管理);
            }
            db.Close();
            if (ioldId == 0)
            {
                LinkService.InsertLog(ESysLogType.Operation, "新增进货单信息[" + ValueHandler.GetStringValue(SM.BBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.进货单管理);
            }
            else
            {
                LinkService.InsertLog(ESysLogType.Operation, "修改进货单信息[" + ValueHandler.GetStringValue(SM.BBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.进货单管理);
            }
            db.Close();
            return blRes;
        }
        public override bool HasExists(object obj)
        {
            BuyBill SM = obj as BuyBill;           
            bool blRes = true;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM BuyBill WHERE BBill_Code='" + SM.BBill_Code + "' AND BBill_Id<>" + SM.BBill_Id + " AND State=1 ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;

            }
            catch (Exception ex)
            {
            }
            db.Close();
            return blRes;
        }

        public override bool Delete(object obj)
        {
            DataBase db = new DataBase();
            bool blRes = false;
            try
            {
                db.Open();
                BuyBill SM = obj as BuyBill;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" UPDATE BuyBill SET ");
                sbSql.Append(" State=0 ");
                sbSql.Append(" WHERE BBill_Id=" + SM.BBill_Id );
                sbSql.Append(" UPDATE BuyBillDetail SET ");
                sbSql.Append(" State=0 ");
                sbSql.Append(" WHERE BBDeta_SBill_Id="+SM.BBill_Id);
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "删除进货单信息[" + ValueHandler.GetStringValue(SM.BBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.进货单管理);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "删除进货单信息！[" + ex.ToString() + "]", EMenuList.进货单管理);
            }
            db.Close();

            return blRes; ;
        }

        public override System.Data.DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT BBill_Id , BBill_Code,BBill_Supp_Id,B.Supp_Name,BBill_Date,BBill_Money,BBill_Count,BBill_SUser_Id,C.SUser_Name,BBill_Input,BBill_Memo,A.State,A.CreateTime,A.CreateMan,case BBill_Input when 0 then '未入库' else '已入库' end as BBill_InputName,A.BBilll_PayType,A.BBill_AList_Id,A.BBill_State,A.BBill_ARecoId FROM BuyBill A ");
                sbSql.Append(" LEFT JOIN Supplier B ON (A.BBill_Supp_Id=B.Supp_Id)");
                sbSql.Append(" LEFT JOIN SysUser C ON (A.BBill_SUser_Id=C.SUser_Id)");
                sbSql.Append(" WHERE A.State=1 ");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "进货列表加载失败！[" + ex.ToString() + "]", EMenuList.进货单管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        /// <summary>
        /// 获取某个进货单明细信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public System.Data.DataTable GetDetail(object obj)
        {
            try
            {
                db = new DataBase();
                BuyBill SM = obj as BuyBill;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT BBill_Id , BBill_Code,BBill_Supp_Id,B.Supp_Name,BBill_Date,BBill_Money,BBill_Count,BBill_SUser_Id,C.SUser_Name,BBill_Input,BBill_Memo,A.State,A.CreateTime,A.CreateMan ,A.BBilll_PayType,A.BBill_AList_Id,A.BBill_State,A.BBill_ARecoId FROM BuyBill A ");
                sbSql.Append(" LEFT JOIN Supplier B ON (A.BBill_Supp_Id=B.Supp_Id)");
                sbSql.Append(" LEFT JOIN SysUser C ON (A.BBill_SUser_Id=C.SUser_Id)");
                sbSql.Append(" WHERE A.State=1 AND BBill_Id=" + SM.BBill_Id);
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.BBill_Code = ValueHandler.GetStringValue(dr["BBill_Code"]);
                    SM.BBill_Count = ValueHandler.GetIntNumberValue(dr["BBill_Count"]);
                    SM.BBill_Date = DateTime.Parse(dr["BBill_Date"].ToString());
                    SM.BBill_Input = ValueHandler.GetIntNumberValue(dr["BBill_Input"]);
                    SM.BBill_Memo = ValueHandler.GetStringValue(dr["BBill_Memo"]);
                    SM.BBill_Money = ValueHandler.GetDecNumberValue(dr["BBill_Money"]);
                    SM.BBill_Supp_Id = ValueHandler.GetIntNumberValue(dr["BBill_Supp_Id"]);
                    SM.BBill_SUser_Id = ValueHandler.GetIntNumberValue(dr["BBill_SUser_Id"]);
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                    SM.BBilll_PayType = ValueHandler.GetIntNumberValue(dr["BBilll_PayType"]);
                    SM.BBill_AList_Id = ValueHandler.GetIntNumberValue(dr["BBill_AList_Id"]);
                    SM.BBill_State = ValueHandler.GetIntNumberValue(dr["BBill_State"]);
                    SM.BBill_ARecoId = ValueHandler.GetIntNumberValue(dr["BBill_ARecoId"]);
                }
                db.Close();
                db = new DataBase();
                sbSql = new StringBuilder();
                sbSql.Append(" SELECT BBDeta_Id  ,BBDeta_SBill_Id,BBDeta_PInfo_Id,BBDeta_Count,BBDeta_CostPrice,BBDeta_Price,BBDeta_Money,BBDeta_Memo ,PInfo_Code,PInfo_Name,PInfo_PSort_ID ,C.PSort_Name FROM BuyBillDetail A ");
                sbSql.Append(" LEFT JOIN ProductInfo B ON (A.BBDeta_PInfo_Id=B.PInfo_Id) " );
                sbSql.Append(" LEFT JOIN ProductSort C ON (B.PInfo_PSort_ID=C.PSort_ID) ");
                sbSql.Append(" WHERE A.BBDeta_SBill_Id="+SM.BBill_Id+" AND A.State=1");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
                db.Data.Tables[0].Columns["BBDeta_Id"].AutoIncrement = true;
                db.Data.Tables[0].Columns["BBDeta_Id"].AutoIncrementStep = 1;
                if (db.Data.Tables[0].Rows.Count <= 0)
                    db.Data.Tables[0].Columns["BBDeta_Id"].AutoIncrementSeed = 1;
                else
                    db.Data.Tables[0].Columns["BBDeta_Id"].AutoIncrementSeed = ValueHandler.GetIntNumberValue(db.Data.Tables[0].Compute("max(BBDeta_Id)+1", ""));

            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "进货列表加载失败！[" + ex.ToString() + "]", EMenuList.进货单管理);
            }
            db.Close();   
            return db.Data.Tables[0];
        }

        public override object GetModel(object obj)
        {
            BuyBill SM = obj as BuyBill;
             try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT BBill_Id , BBill_Code,BBill_Supp_Id,B.Supp_Name,BBill_Date,BBill_Money,BBill_Count,BBill_SUser_Id,C.SUser_Name,BBill_Input,BBill_Memo,A.State,A.CreateTime,A.CreateMan,A.BBilll_PayType,A.BBill_AList_Id,A.BBill_State,A.BBill_ARecoId FROM BuyBill A ");
                sbSql.Append(" LEFT JOIN Supplier B ON (A.BBill_Supp_Id=B.Supp_Id)");
                sbSql.Append(" LEFT JOIN SysUser C ON (A.BBill_SUser_Id=C.SUser_Id)");
                sbSql.Append(" WHERE A.State=1 AND BBill_Id=" + SM.BBill_Id);
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.BBill_Code = ValueHandler.GetStringValue(dr["BBill_Code"]);
                    SM.BBill_Count = ValueHandler.GetIntNumberValue(dr["BBill_Count"]);
                    SM.BBill_Date = DateTime.Parse(dr["BBill_Date"].ToString());
                    SM.BBill_Input = ValueHandler.GetIntNumberValue(dr["BBill_Input"]);
                    SM.BBill_Memo = ValueHandler.GetStringValue(dr["BBill_Memo"]);
                    SM.BBill_Money = ValueHandler.GetDecNumberValue(dr["BBill_Money"]);
                    SM.BBill_Supp_Id = ValueHandler.GetIntNumberValue(dr["BBill_Supp_Id"]);
                    SM.BBill_SUser_Id = ValueHandler.GetIntNumberValue(dr["BBill_SUser_Id"]);
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                    SM.BBilll_PayType = ValueHandler.GetIntNumberValue(dr["BBilll_PayType"]);
                    SM.BBill_AList_Id = ValueHandler.GetIntNumberValue(dr["BBill_AList_Id"]);
                    SM.BBill_State = ValueHandler.GetIntNumberValue(dr["BBill_State"]);
                    SM.BBill_ARecoId = ValueHandler.GetIntNumberValue(dr["BBill_ARecoId"]);
                }              
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "进货单明细加载失败！[" + ex.ToString() + "]", EMenuList.进货单管理);
            }
            db.Close();
            return SM;
        }
        
        /// <summary>
        /// 将某个进货单进行入库
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool PutIn(BuyBill obj)
        {
            //获取明细信息
            bool blRes = false;
            StringBuilder sbSql = new StringBuilder();
            BuyBill bb = obj as BuyBill;
            db = new DataBase();
            try
            {
                DataTable dtList = GetDetail(obj);
                db.Open();
                for (int i = 0; i < dtList.Rows.Count; i++)
                {
                    sbSql = new StringBuilder();
                    string strSDeta_Stock_Id = "1";//仓库默认为1
                    int iSDeta_PInfo_Id = ValueHandler.GetIntNumberValue(dtList.Rows[i]["BBDeta_PInfo_Id"]);
                    int iBBDeta_Count = ValueHandler.GetIntNumberValue(dtList.Rows[i]["BBDeta_Count"]);

                    sbSql.Append(@"  IF EXISTS(SELECT * FROM StockDetail WHERE SDeta_PInfo_Id=" + iSDeta_PInfo_Id.ToString() + @")
                                BEGIN
	                                UPDATE StockDetail SET SDeta_RealNumber=SDeta_RealNumber+" + iBBDeta_Count.ToString() + @",SDeta_InputNumber=SDeta_InputNumber+" + iBBDeta_Count.ToString() + @" WHERE SDeta_PInfo_Id=" + iSDeta_PInfo_Id.ToString() + @"
                                END
                                ELSE
                                BEGIN
	                                INSERT INTO StockDetail (SDeta_Stock_Id,SDeta_PInfo_Id,SDeta_InputNumber,SDeta_OutputNumber,SDeta_RealNumber,State,CreateTime,CreateMan)
                                SELECT SDeta_Stock_Id=" + strSDeta_Stock_Id + @",
                                SDeta_PInfo_Id=" + iSDeta_PInfo_Id.ToString() + @",
                                SDeta_InputNumber=" + iBBDeta_Count.ToString() + @",
                                SDeta_OutputNumber=0,
                                SDeta_RealNumber=" + iBBDeta_Count.ToString() + @",
                                State=1,
                                CreateTime=GETDATE(),
                                CreateMan=1
                                END ");
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    //入库后，将产品基本信息表中的进价和零售价修改为本次的进价和零售价
                    sbSql = new StringBuilder();
                    sbSql.Append(" UPDATE ProductInfo SET PInfo_Cost=" + ValueHandler.GetDecNumberValue(dtList.Rows[i]["BBDeta_CostPrice"]).ToString()+",");
                    sbSql.Append(" PInfo_Price=" + ValueHandler.GetDecNumberValue(dtList.Rows[i]["BBDeta_Price"]).ToString());
                    sbSql.Append(" WHERE PInfo_Id=" + iSDeta_PInfo_Id);
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());                    

                }
                //修改进货单主表状态
                SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, "UPDATE BuyBill SET BBill_Input=1 WHERE BBill_Id="+bb.BBill_Id.ToString());
                db.Transaction.Commit();
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "进货单["+bb.BBill_Code+"]入库完成！", EMenuList.进货单管理);
            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Operation, "进货单[" + bb.BBill_Code + "]入库失败！"+ex.ToString(), EMenuList.进货单管理);

            }
            db.Close();
            return blRes;
        }
        public bool JS(object obj)
        {
            BuyBill SModel = obj as BuyBill;
            AccountRecord SM = new AccountRecord();
            SM.AReco_Code = Coder.CreateCoder(EMenuList.转账管理);
            SM.AReco_Datetime = DateTime.Parse(System.DateTime.Now.ToString("yyyy-MM-dd"));
            SM.AReco_InNumber = -1;
            SM.AReco_Kind = 2;
            SM.AReco_Man = LinkService.GetCurrentUser().SUser_Id;
            SM.AReco_Noney = SModel.BBill_Money;
            SM.AReco_OutNumber = SModel.BBill_AList_Id;
            SM.AReco_Reason = "进货单[" + SModel.BBill_Code + "]结算，结算金额：" + SModel.BBill_Money;
            SM.AReco_Type = 2;
            SM.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SM.CreateTime = System.DateTime.Now;
            SM.State = 1;
            AccountRecordOp accountRecordManager = new AccountRecordOp();
            bool blRes = accountRecordManager.Save(SM);
            if (blRes)
            {
                try
                {
                    StringBuilder sbSql = new StringBuilder();
                    sbSql.Append(" UPDATE BuyBill SET BBill_State = 1 , BBill_ARecoId=" + SM.ARecoId + " WHERE BBill_Id=" + SModel.BBill_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                    blRes = true;
                    LinkService.InsertLog(ESysLogType.Operation, "进货单[" + SModel.BBill_Code + "]结算完成！", EMenuList.进货单管理);
                }
                catch (Exception ex)
                {
                    blRes = false;
                }
                db.Close();
            }
            return blRes;
        }
    }
}
